import { createRouter, createWebHistory } from 'vue-router'
import ArticleChannel from '@/views/article/ArticleChannel.vue'
import ArticleManage from '@/views/article/ArticleManage.vue'
import LoginPage from '@/views/login/LoginPage.vue'
import LayoutContainer from '@/views/layout/LayoutContainer.vue'
import UserAvatar from '@/views/user/UserAvatar.vue'
import UserPassword from '@/views/user/UserPassword.vue'
import UserProfile from '@/views/user/UserProfile.vue'
import {useUserStore} from '@/stores'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {path:'/login',component:LoginPage },
    {path:'/',component:LayoutContainer,
      redirect: '/article/manage',
      children: [
        {path:'/article/manage',component:ArticleManage},
        {path:'/article/channel',component:ArticleChannel},
        {path:'/user/avatar',component:UserAvatar},
        {path:'/user/password',component:UserPassword},
        {path:'/user/profile',component:UserProfile},
      ]
    }
  ]
})
// 默认直接放行
// 根据返回值决定是放行还是拦截
// 1.undefined/true 直接放行
// 2,false 拦回from的地址页面
// 3.具体路径或路径对象拦截到对应的地址
// '/login'  {name:'login'}
router.beforeEach((to,from)=>{
  // 如果没有token,且访问的是非登录页，那么拦截到登录，其他情况正常放行
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login') return '/login'
  return true
})
export default router
